#! /usr/bin/python

################################################################################
#	\file   SConscript
#	\author Gregory Diamos <solusstultus@gmail.com>
#   \date   Saturday August 10, 2013
#	\brief  The SCons master build script for libcuxx
################################################################################

import os

# set environment variables
Import('nvenv')

env = nvenv

# helper functions
def findLibraryPath():
	return os.path.realpath(os.path.join(env['path_to_root_dir'],
		'libcxx', 'libcxx'))

# find all source files in the source tree
libraryDirectory = findLibraryPath()

if env['install']:
	directories = ['libcxx/src']

	source_directories = directories
	extensions = ['*.cpp']

	sources = []
	for dir in source_directories:
		for ext in extensions:
			regexp = os.path.join(dir, ext)
			sources.extend(env.Glob(regexp))
	
	# create the library
	libcxx = env.SharedLibrary('libcxx', sources)
	libcxx_libs = ['libcxx'] + env['EXTRA_LIBS']
	
	libcxx = env.Install(os.path.join(env['install_path'], "lib"),
		libcxx)

	env.Depends(libcxx, os.path.join(env['install_path'], "include"))

	# create the tests
	tests = []

	#tests.append(env.Program('gpu-native-loader', \
	#	['gpu-native/tools/gpu-native-loader.cpp'], LIBS=gpunative_libs))

	for test in tests:
		env.Depends(test, libcxx)

# install it all 
if env['install']:
	installed   = []
	executables = []

	print 'Installing libcxx'

	for test in tests:
		executables.append(env.Install(
			os.path.join(env['install_path'], "test"), test))
	
	# install headers
	header_directories = [os.path.join(libraryDirectory, 'include')]
	header_extensions = ['*']
	
	headers = []
	for dir in header_directories:
		for ext in header_extensions:
			regexp = os.path.join(dir, ext)
			headers.extend(env.Glob(regexp))
	
	for header in headers:
		(directoryPath, headerName) = os.path.split( \
			os.path.relpath(str(header), libraryDirectory))
		installed.append(env.Install(os.path.join( \
			env['install_path'], "include", 'libcxx', directoryPath), header))
	
	# set permissions
	for i in executables:
		env.AddPostAction(i, Chmod(i, 0755))
	for i in installed:
		env.AddPostAction(i, Chmod(i, 0644))



